package com.wooophone.mpyeah.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.wooophone.mpyeah.dao.BrandCategoryDao;
import com.wooophone.mpyeah.dao.BrandCategoryFKDao;
import com.wooophone.mpyeah.dao.BrandCommentDao;
import com.wooophone.mpyeah.dao.BrandCommentReplyDao;
import com.wooophone.mpyeah.dao.BrandDao;
import com.wooophone.mpyeah.entity.Brand;
import com.wooophone.mpyeah.entity.BrandCategory;
import com.wooophone.mpyeah.entity.BrandCategoryFK;
import com.wooophone.mpyeah.entity.BrandComment;
import com.wooophone.mpyeah.mapper.BrandMapper;

public class BrandDaoImpl implements BrandDao {

	@Autowired
	private BrandMapper brandMapper;

	@Autowired
	private BrandCategoryFKDao brandCategoryFKDao;
	@Autowired
	BrandCategoryDao brandCategoryDao;
	@Autowired
	BrandCommentDao brandCommentDao;
	@Autowired
	BrandCommentReplyDao brandCommentReplyDao;

	public void setBrandCategoryFKDao(BrandCategoryFKDao brandCategoryFKDao) {
		this.brandCategoryFKDao = brandCategoryFKDao;
	}

	public void setBrandMapper(BrandMapper brandMapper) {
		this.brandMapper = brandMapper;
	}

	@Override
	public Brand save(Brand obj) {
		brandMapper.save(obj);
		this.saveFK(obj.getCategorySet(), obj.getId());
		return obj;
	}

	@Override
	public int update(Brand obj) {
		Set<BrandCategory> set = obj.getCategorySet();
		brandCategoryFKDao.deleteByBrandId(obj.getId());// 删除外键
		saveFK(set, obj.getId());
		return brandMapper.update(obj);
	}

	private void saveFK(Set<BrandCategory> set, String brandId) {
		Iterator<BrandCategory> iter = set.iterator();
		BrandCategoryFK brandCategoryFK = new BrandCategoryFK();
		brandCategoryFK.setBrandId(brandId);
		while (iter.hasNext()) {
			brandCategoryFK.setCategoryId(iter.next().getId());
			brandCategoryFKDao.save(brandCategoryFK);
		}
	}

	@Override
	public int delete(String id) {
		brandCategoryFKDao.deleteByBrandId(id);// 删除外键
		try {
			brandCommentDao.deleteByBrandId(id);		
		} catch (Exception e) {
			e.printStackTrace();
		}finally{	//如果有外键关系删除失败则删除外键后 再删除品牌 否则一步通过  因为级联删除可以在数据库中配置实现 
			Map<String, Object> parameter=new HashMap<String, Object>();
			parameter.put("brandId", id);
			List<BrandComment> list=brandCommentDao.find(parameter);
			for (BrandComment brandComment : list) {
				brandCommentReplyDao.deleteByCommentId(brandComment.getId());
			}
			brandCommentDao.deleteByBrandId(id);
		}
		return brandMapper.delete(id);
	}

	@Override
	public Brand get(String id) {
		Brand brand = brandMapper.get(id);
//		List<BrandCategoryFK> list = brandCategoryFKDao.listByBrandId(id);
//		if (list != null) {
//			Iterator<BrandCategoryFK> iter = list.iterator();
//			Set<BrandCategory> set = new HashSet<BrandCategory>();
//			while (iter.hasNext()) {
//				set.add(brandCategoryDao.get(iter.next().getCategoryId()));
//			}
//			brand.setCategorySet(set);
//		}else{
//			brand.setCategorySet(null);
//		}
		return brand;
	}

	@Override
	public List<Brand> list() {
		return brandMapper.list();
	}

	@Override
	public List<Brand> list(int offset, int limit) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("offset", offset);
		parameter.put("limit", limit);
		return brandMapper.listByLimit(parameter);
	}

	@Override
	public int count() {
		return brandMapper.count();
	}

	@Override
	public List<Brand> find(Map<String, Object> parameter) {
		return brandMapper.listByParameter(parameter);
	}

	@Override
	public List<Brand> find(Map<String, Object> parameter, int offset, int limit) {
		parameter.put("limit", limit);
		parameter.put("offset", offset);
		return brandMapper.listByParameter(parameter);
	}

	@Override
	public int count(Map<String, Object> parameter) {
		return brandMapper.countByParameter(parameter);
	}

	@Override
	public int count(String keyword) {
		keyword = "%" + keyword + "%";
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("keyword", keyword);
		return brandMapper.countByKeyword(parameter);
	}

	@Override
	public List<Brand> search(String keyword, int offset, int limit) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("limit", limit);
		parameter.put("offset", offset);
		parameter.put("keyword", "%" + keyword + "%");
		return brandMapper.search(parameter);
	}

	@Override
	public List<Brand> findByCids(List<String> cids, int offset, int limit) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("limit", limit);
		parameter.put("offset", offset);
		parameter.put("cids", cids);
		return brandMapper.findByCids(parameter);
	}

	@Override
	public int countByCids(List<String> cids) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("cids", cids);
		return brandMapper.countByCids(parameter);
	}

	@Override
	public List<Brand> findRecommendBrand(int offset, int limit) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("limit", limit);
		parameter.put("offset", offset);
		return brandMapper.findRecommendBrand(parameter);
	}

	@Override
	public Brand findByName(String name) {
		return this.brandMapper.findByName(name);
	}

	public void setBrandCategoryDao(BrandCategoryDao brandCategoryDao) {
		this.brandCategoryDao = brandCategoryDao;
	}
	
	
	public List<BrandCategory> findCategoryList(String id){
		Set<BrandCategory> set=new HashSet<BrandCategory>();
		List<BrandCategoryFK> list=brandCategoryFKDao.listByBrandId(id);
		List<String> cids=new ArrayList<String>();
		for (BrandCategoryFK brandCategoryFK : list) {
			cids.add(brandCategoryFK.getCategoryId());
		}
		if(cids.size()<1){
			return null;
		}
		List<BrandCategory> categoryList=brandCategoryDao.findByCategoryIds(cids);
		
		return categoryList;
	}

	public void setBrandCommentDao(BrandCommentDao brandCommentDao) {
		this.brandCommentDao = brandCommentDao;
	}

	public void setBrandCommentReplyDao(BrandCommentReplyDao brandCommentReplyDao) {
		this.brandCommentReplyDao = brandCommentReplyDao;
	}

	
}
